import { DataSource } from 'typeorm';
import { User } from '../users/entities/user.entity';
import { Asset } from '../assets/entities/asset.entity';
import { Application } from '../applications/entities/application.entity';
import { KycSubmission } from '../kyc/entities/kyc-submission.entity';
import { OtcOrder } from '../otc/entities/otc-order.entity';
import { TxJob } from '../blockchain/entities/tx-job.entity';

async function syncDatabase() {
    const dataSource = new DataSource({
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'rwa_user',
        password: 'rwa_password',
        database: 'rwa_platform',
        entities: [User, Asset, Application, KycSubmission, OtcOrder, TxJob],
        synchronize: true,
        logging: true,
    });

    try {
        await dataSource.initialize();
        console.log('✅ Database connection established');

        // 同步数据库结构
        await dataSource.synchronize();
        console.log('✅ Database schema synchronized');

        // 检查表是否创建成功
        const tables = await dataSource.query(`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'public'
      ORDER BY table_name;
    `);

        console.log('📋 Created tables:');
        tables.forEach((table: any) => {
            console.log(`  - ${table.table_name}`);
        });

        await dataSource.destroy();
        console.log('✅ Database connection closed');

    } catch (error) {
        console.error('❌ Database sync failed:', error);
        process.exit(1);
    }
}

syncDatabase();
